Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
There are 2 options
- To split the string by regular expression
"[A-Z]"
and then print the length of an array returned from splitting. - To iterate over all characters, count the uppercase and print their
count + 1
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.*; public class CamelCase { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String text = stdin.nextLine(); String[] words = text.split("[A-Z]"); System.out.println(words.length); stdin.close(); } } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
'use strict'; const processData = input => { const line = input; const words = line.split(/[A-Z]/g); console.log(words.length); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 |
object CamelCase { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in) val s = sc.next() println(s.split("[A-Z]").length) } } |
Ruby
1 2 3 |
text = gets.strip words = text.split(/[A-Z]/) puts words.size |